#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

int n, m, k;
int x[N], y[N], z[N];
ll fastpow(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % MOD;
    b = b * b % MOD;
    p >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m >> k;
    rep(i, 1, k) cin >> x[i] >> y[i] >> z[i];
    int bcnt = 0, wcnt = 0;
    rep(i, 1, k) {
      int f1 = x[i] == 1 || x[i] == n;
      int f2 = y[i] == 1 || y[i] == m;
      if (f1 ^ f2) {
        if (z[i])
          bcnt++;
        else
          wcnt++;
      }
    }
    ll tot = 2ll * (n - 2) + 2ll * (m - 2);
    if (bcnt + wcnt == tot) {
      if (bcnt % 2 == 0)
        cout << fastpow(2, 1ll * n * m - k) << endl;
      else
        cout << 0 << endl;
    } else
      cout << fastpow(2, 1ll * n * m - k - 1) << endl;
  }
  return 0;
}